<div class="tab">
    <div class="tab_item">
		<?php 
            echo CHtml::link(
                "Danh sách nhóm", 
                array($controller_id."/view"),
                array("class"=>"tab_item")
            );
        ?>
    </div>
    
    <div class="tab_item">
		<?php 
            echo CHtml::link(
                "Thêm mới", 
                array($controller_id."/add"),
                array("class"=>"tab_item")
            );
        ?>
    </div>
    
    <div class="tab_item">
		<?php 
            echo CHtml::link(
                "Bảng phân quyền", 
                array($controller_id."/view_grant", "current_group_id" => $current_group["id"]),
                array("class"=>"tab_item active")
            );
        ?>
    </div>
    
    
</div> <!-- end div tab -->


<style>
.phan_quyen{
	width:70%;
}

.phan_quyen table{
	border-collapse:collapse;
	border:1px solid #eee;
}
.phan_quyen table th{
	border-collapse:collapse;
	border:1px solid #dbecfd;
	background-color:#e3f0fd;
}
.phan_quyen table td{
	border-collapse:collapse;
	border:1px solid #dbecfd;
}

</style>
<div class="right">
	<div class="content">
    <?php
		 echo CHtml::beginForm(array($controller_id."/view_grant"),"post");
        echo "<table cellpadding='3' cellspacing='0 border='0' width='50%'>";
			echo "<tr>";
				echo "<td class='label' width='100'>"."Chọn nhóm"."</td>";
				echo "<td>";
				$list_group = array();
				$list_group[0] = "-- Chọn nhóm --";
				
				$groups = group::model()->findAll();
				if (! is_null($groups))
				{
					foreach($groups as $g)
						$list_group[$g["id"]] = $g["group_name"];
				}
				echo CHtml::dropDownList("group_id", $current_group["id"], $list_group, array("id"=>"current_group_id"));
				echo "</td>";
			echo "</tr>";
		echo "</table>";
		echo CHtml::endForm();
	?>
     <script language="javascript">
		$(document).ready(function(){
			$('#current_group_id').change(function(){
				var form = document.forms[0];
				form.action = form.action + "&current_group_id=" + this.value ;
				form.submit();
			});	
		})
      </script>
	<p>Bảng phân quyền cho nhóm: <strong><?php echo $current_group["group_name"] ;?> </strong></p>
		
	<div class="phan_quyen">
    	<script language="javascript">
			$(function() {
				$( "#accordion" ).accordion();
			});
		</script>
    	<div id="accordion">
            <h3><a href="#">Danh sách quyền</a></h3>
            <div style="padding:0px">
                 <table cellpadding="3" cellspacing="0" width="100%">
                    <thead>
                        <th width="30">#</th>
                        <th>Admin Menu</th>
                        <th width="80">Bật/Tắt</th>
                        <th width="150">Controller_id</th>
                         <th>ID</th>
                        <th width="20"></th>
                    </thead>
                    <tbody>
					<?php
					  if (! is_null($current_group))
					  {
						  $array_id = array();
						  $adminmenus_parent = $current_group->adminmenus(array(
						  	"condition" => "parent_id = 0"
						  ));
						  
						  $i = 1;
						  if (! is_null($adminmenus_parent))
						  {
							  
							  foreach($adminmenus_parent as $row_parent)
							  {
								  show_row($row_parent, $current_group,$controller_id, $i++);
								  $array_id[$row_parent->id] = $row_parent->controller_id;
								  $adminmenus_children = $current_group->adminmenus(array(
								  	"condition" => "parent_id = " . $row_parent["id"]
								  ));
								  
								  if (!is_null($adminmenus_children))
								  {
									  foreach($adminmenus_children as $adminmenus_child)
									  {
										  show_row($adminmenus_child, $current_group,$controller_id, $i++);
										  $array_id[$adminmenus_child->id] = $adminmenus_child->controller_id;
									  }
								  }
							  }
						  }
						  
						  $adminmenus_other = $current_group->adminmenus(array(
						  	"condition" => "parent_id <> 0"
						  ));
						  
						  if (! is_null($adminmenus_other))
						  {
							  foreach($adminmenus_other as $row)
							  {
								  if (! array_key_exists($row->id, $array_id))
								  	show_row($row, $current_group,$controller_id, $i++);
							  }
						  }
						  
					  }
					  
					  function show_row($row, $current_group, $controller_id, $i)
					  {
						  echo "<tr>";
								  
							echo "<td align='center'>".$i."</td>";
							
							echo "<td>";
								if ($row["parent_id"] > 0)
									echo CHtml::image(Yii::app()->baseUrl.'/assets/icon/icon-16-triangle.png');
								echo $row["name_default"];
							echo "</td>";
							
							echo "<td align='center'>";
								$icons =  array(
									0 => "icon-16-remove.png",
									1 => "icon-16-tick.png",
								);
								echo CHtml::image(Yii::app()->baseUrl.'/assets/icon/'.$icons[$row["enable"]]);
							echo "</td>";
							
							echo "<td>";
								echo $row["controller_id"];
							echo "</td>";
							
							echo "<td align='center'>";
								echo $row["id"];
							echo "</td>";
							
							echo "<td align='center'>";
							echo CHtml::link(
								CHtml::image(Yii::app()->baseUrl.'/assets/icon/icon-16-trash.png'),
								array(
									$controller_id."/remove_grant",
									"current_group_id"=> $current_group["id"],
									"adminmenu_id" => $row["id"]
								),
								array("title"=>"Remove")
							);
							echo "</td>";
						  echo "</tr>";
					  }
					  
                    ?>
                    </tbody>
                </table>
                <p>
                <?php
                    echo "&nbsp;";
                    echo CHtml::link(
                        CHtml::button("Thêm"),
                        array(
                            $controller_id."/view_adminmenu",
                            "current_group_id" => $current_group["id"]
                        ),
                        array('title'=>'Thêm')
                    );
                ?>
                </p>
            </div>
          
           
        </div><!-- End id accordion -->
    </div> <!-- end div phan_quyen -->
    	
    </div>
</div><!-- end div right -->